#!/usr/bin/env bash
#######################################
aria2_download_termux() {
    cd /sdcard/Download
    if [ -f "com.termux_Fdroid.apk" ]; then
        if (whiptail --title "检测到文件已下载,请选择您需要执行的操作！" --yes-button 'install(*￣▽￣*)o' --no-button 'Upgrade(っ °Д °)' --yesno "Detected that the file has been downloaded, do you want to install it, or download it again?" 7 60); then
            install_termux_apk
        else
            download_termux_apk_again
        fi
    else
        download_termux_apk_again
    fi
    return 0
}
#######################################
download_termux_apk_again() {
    printf "%s\n" "${YELLOW}按回车键开始下载${RESET}"
    do_you_want_to_continue
    printf '%s\n' 'Downloading termux apk...'
    printf '%s\n' '正在为您下载至/sdcard/Download目录...'
    printf '%s\n' '下载完成后，需要您手动安装。'
    aria2c --console-log-level=warn --no-conf -x 16 -k 1M --split=16 --allow-overwrite=true -o "com.termux_Fdroid.apk" 'https://apk.tmoe.me/termux' || aria2c --console-log-level=warn --no-conf -x 16 -k 1M --split=16 --allow-overwrite=true -o "com.termux_Fdroid.apk" 'https://apk.tmoe.me/termux'
    install_termux_apk
}
install_termux_apk() {
    printf "%s\n" "${YELLOW}下载完成，请进入下载目录手动安装。${RESET}"
    am start -n com.android.documentsui/com.android.documentsui.ViewDownloadsActivity
    cd ${CURRENT_DIR}
}
##################################
aria2_download_termux $@
